package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.BussinessAccusation;
import com.sl.au.entity.Option;
import com.sl.au.service.BussinessAccusationService;

import com.sl.utils.MsgHelper;

import com.sl.au.service.OptionService;


@RestController
@RequestMapping(value = "/ui/bussinessAccusation")
public class BussinessAccusationController extends BaseController {

	@Autowired
	private BussinessAccusationService BussinessAccusationService;

	
	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public MsgHelper Delete(@PathVariable(value = "id") String id) {
		BussinessAccusationService.deleteById(id);
		return MsgHelper.success();
	}


    @Autowired
    private OptionService optionService;
	

	@RequestMapping(value = "/page", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<BussinessAccusation> accusations = null;
		String key = request.getParameter("key");
		String device = request.getParameter("device");
		String identify = request.getParameter("identify");
		// 移动端
		if (this.DEVICE.equals(device) && StringUtils.hasText(device)) {
			accusations = BussinessAccusationService.getAccusations(start, length, identify, null, null);
		} else {
			// PC端

			if (StringUtils.hasText(key)) {
				accusations = BussinessAccusationService.getAccusations(start, length, key, null, null);
			} else {
				accusations = BussinessAccusationService.getPage(start, length, null, null, null);
			}
		}
		int count = BussinessAccusationService.getCount(key);
		map.put("data", accusations);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	@RequestMapping(value = "receiveThing", method = RequestMethod.POST)
	public Map<String, Object> receiveThing(@RequestBody Map<String, Object> request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id =(String)request.get("id");
		int code =this.BussinessAccusationService.receiveThing(id, this.getMyAccount().getUser());
		map.put("code", code);
		return map;
	}
	
	@RequestMapping(value = "finishThing", method = RequestMethod.POST)
	public Map<String, Object> finishThing(@RequestBody Map<String, Object> request) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String id =(String)request.get("id");
		String resultTypeId =(String)request.get("resultTypeId");
		String memo =(String)request.get("memo");
		
		int code =this.BussinessAccusationService.finishThing(id,this.getMyAccount().getUser(),memo,resultTypeId);
		map.put("code", code);
		return map;
	}
	@RequestMapping(method = RequestMethod.GET)

	public ModelAndView index(ModelAndView modelAndView) {
		String device = request.getParameter("device");
		String url = request.getParameter("url");
		String identify = request.getParameter("identify");
		// 移动端
		if (StringUtils.hasText(device) && this.DEVICE.equals(device)) {
			// 移动端访问的首页
			modelAndView.setViewName("mobile/bussinessAccusation/indexOnPhone");
			modelAndView.addObject("bussinessId", identify);
			modelAndView.addObject("device", device);
			modelAndView.addObject("url", url);
		} else {
			// PC端
			modelAndView = createLayoutView("bussinessAccusation/bussinessAccusation");
			List<Option> options= this.optionService.getRepository().findByGroup("投诉建议处理结果");
			modelAndView.addObject("options", options);
		}

	
		return modelAndView;
	}
}
